<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品评价系统 - 首页</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .hero-section {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 100px 0;
        }
        .feature-card {
            transition: transform 0.3s;
            border: none;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }
        .feature-card:hover {
            transform: translateY(-5px);
        }
        .stats-section {
            background-color: #f8f9fa;
            padding: 60px 0;
        }
        .product-card {
            transition: transform 0.3s;
            border: none;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .product-card:hover {
            transform: translateY(-3px);
        }
        .product-image {
            height: 200px;
            object-fit: cover;
        }
    </style>
</head>
<body>
    <!-- 引入通用头部 -->
    <%@ include file="common/header.jsp" %>

    <!-- 英雄区域 -->
    <section class="hero-section text-center">
        <div class="container">
            <h1 class="display-4 mb-4">欢迎来到商品评价系统</h1>
            <p class="lead mb-4">发现优质商品，分享真实评价，让购物更明智</p>
            <a href="${ctx}/page/products" class="btn btn-light btn-lg">
                <i class="fas fa-search me-2"></i>浏览商品
            </a>
        </div>
    </section>

    <!-- 热门商品 -->
    <section class="py-5">
        <div class="container">
            <h2 class="text-center mb-5">热门商品</h2>
            <div class="row" id="hotProducts">
                <!-- 商品卡片将通过JavaScript动态加载 -->
            </div>
            <div class="text-center mt-4">
                <a href="${ctx}/page/products" class="btn btn-primary">
                    <i class="fas fa-arrow-right me-2"></i>立即体验
                </a>
            </div>
        </div>
    </section>

    <!-- 统计信息 -->
    <section class="bg-light py-5">
        <div class="container">
            <div class="row text-center">
                <div class="col-md-4 mb-4">
                    <div class="card border-0 bg-transparent">
                        <div class="card-body">
                            <i class="fas fa-users fa-3x text-primary mb-3"></i>
                            <h3 class="card-title" id="userCount">0</h3>
                            <p class="card-text">注册用户</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 mb-4">
                    <div class="card border-0 bg-transparent">
                        <div class="card-body">
                            <i class="fas fa-box fa-3x text-success mb-3"></i>
                            <h3 class="card-title" id="productCount">0</h3>
                            <p class="card-text">商品数量</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 mb-4">
                    <div class="card border-0 bg-transparent">
                        <div class="card-body">
                            <i class="fas fa-star fa-3x text-warning mb-3"></i>
                            <h3 class="card-title" id="reviewCount">0</h3>
                            <p class="card-text">用户评价</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- 页脚 -->
    <footer class="footer">
        <div class="container">
            <div class="row">
                <div class="col-md-6">
                    <h5>商品评价系统</h5>
                    <p>基于SSM框架开发的商品评价系统，提供完整的商品管理和评价功能。</p>
                </div>
                <div class="col-md-6">
                    <h5>快速链接</h5>
                    <ul class="list-unstyled">
                        <li><a href="${ctx}/page/products" class="text-light">商品列表</a></li>
                <li><a href="${ctx}/page/login" class="text-light">用户登录</a></li>
                <li><a href="${ctx}/page/register" class="text-light">用户注册</a></li>
                    </ul>
                </div>
            </div>
            <hr class="my-4">
            <div class="text-center">
                <p>&copy; 2024 商品评价系统. All rights reserved.</p>
            </div>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            // 加载推荐商品
            loadRecommendedProducts();
            
            // 加载统计数据
            loadStats();
        });
        
        function loadRecommendedProducts() {
            apiRequest({
                url: AppConfig.APIS.PRODUCT.PAGE,
                type: 'GET',
                data: {
                    page: 1,
                    size: 6
                },
                success: function(response) {
                    if (response.success && response.data && response.data.content) {
                        displayProducts(response.data.content);
                    }
                },
                error: function() {
                    console.error('加载推荐商品失败');
                }
            });
        }
        
        function displayProducts(products) {
            const container = $('#recommendedProducts');
            container.empty();
            
            products.forEach(function(product) {
                const productCard = `
                    <div class="col-md-4 mb-4">
                        <div class="card product-card h-100">
                            <img src="\${product.imageUrl || buildStaticUrl('/img/default-product.jpg')}"
                                 class="card-img-top product-image" alt="\${product.name}">
                            <div class="card-body d-flex flex-column">
                                <h5 class="card-title">\${product.name}</h5>
                                <p class="card-text text-muted">\${product.description || '暂无描述'}</p>
                                <div class="mt-auto">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <span class="h5 text-primary mb-0">¥\${product.price}</span>
                                        <div class="rating">
                                            <i class="fas fa-star"></i>
                                            <span>\${product.averageRating || '暂无评分'}</span>
                                        </div>
                                    </div>
                                    <a href="\${buildPageUrl('/product-detail?id=' + product.id)}"
                                       class="btn btn-primary btn-sm mt-2 w-100">
                                        查看详情
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                `;
                container.append(productCard);
            });
        }
        
        function loadStats() {
            // 加载用户数量
            apiRequest({
                url: AppConfig.APIS.USER.COUNT,
                type: 'GET',
                success: function(response) {
                    if (response.success) {
                        $('#userCount').text(response.data || 0);
                    }
                }
            });
            
            // 加载商品数量
            apiRequest({
                url: AppConfig.APIS.PRODUCT.COUNT,
                type: 'GET',
                success: function(response) {
                    if (response.success) {
                        $('#productCount').text(response.data || 0);
                    }
                }
            });
            
            // 加载评价数量
            apiRequest({
                url: AppConfig.APIS.REVIEW.COUNT,
                type: 'GET',
                success: function(response) {
                    if (response.success) {
                        $('#reviewCount').text(response.data || 0);
                    }
                }
            });
        }
    </script>
</body>
</html>